package edu.scu.declaresys.web.action;

import java.util.Date;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

import edu.scu.declaresys.config.IAttrName;
import edu.scu.declaresys.dao.util.Depth;
import edu.scu.declaresys.service.ServiceFactory;
import edu.scu.declaresys.service.intr.IProjectService;
import edu.scu.declaresys.util.PageList;
import edu.scu.declaresys.vo.Project;
import edu.scu.declaresys.web.ABaseAction;

public class AuditProjectAction extends ABaseAction implements
		ModelDriven<Project>
{
	private static final long	serialVersionUID	= -1725668146331569127L;
	
	private IProjectService projectService = ServiceFactory.createProjectService();
	
	private Project project = new Project();
	
	private Integer page;
	
	//
	// 待审批
	//
	public String auditWaitAuditProject() throws Exception
	{
		PageList<Project> projectLst = projectService.findWaitAuditProject(project.getProjectClassId(),
				project.getYear(), project.getName(), page);
		request.setAttribute(IAttrName.PROJECT_LIST, projectLst);
		request.setAttribute(IAttrName.PROJECT_LIST_SIZE, projectLst.size());
		request.setAttribute(IAttrName.CLASS_LIST, ServiceFactory.createProjectClassService().findClassList());
		return Action.SUCCESS;
	}
	
	public String auditPass() throws Exception
	{
		if(project.getId() == null)
		{
			throw new Exception("未指定项目");
		}
		/**
		 * validation.
		 */
		Project dbProject = projectService.findById(project.getId(), Depth.ZERO);
		if(dbProject == null)
		{
			throw new Exception("指定的项目不存在");
		}
		// Check state.
		int state = dbProject.getState();
		if(state != Project.STATE_REQUEST_MODIFY
				&& state != Project.STATE_UNAUDIT_MODIFIABLE
				&& state != Project.STATE_UNAUDIT_UNMODIFIABLE)
		{
			throw new Exception("该项目所处的状态不可以进行审批");
		}
		/**
		 * update project state.
		 */
		Project newProject = new Project();
		newProject.setId(dbProject.getId());
		newProject.setState(Project.STATE_PASSED);
		newProject.setAuditTime(new Date());
		//
		projectService.update(newProject);
		return Action.SUCCESS;
	}
	
	public String auditReject() throws Exception
	{
		if(project.getId() == null)
		{
			throw new Exception("未指定项目");
		}
		/**
		 * validation.
		 */
		Project dbProject = projectService.findById(project.getId(), Depth.ZERO);
		if(dbProject == null)
		{
			throw new Exception("指定的项目不存在");
		}
		// Check state.
		int state = dbProject.getState();
		if(state != Project.STATE_REQUEST_MODIFY
				&& state != Project.STATE_UNAUDIT_MODIFIABLE
				&& state != Project.STATE_UNAUDIT_UNMODIFIABLE)
		{
			throw new Exception("该项目所处的状态不可以进行审批");
		}
		/**
		 * update project state.
		 */
		Project newProject = new Project();
		newProject.setId(dbProject.getId());
		newProject.setState(Project.STATE_REJECTED);
		newProject.setAuditTime(new Date());
		//
		projectService.update(newProject);
		return Action.SUCCESS;
	}
	
	//
	// 已审批
	//
	public String auditHasAudittedProject() throws Exception
	{
		PageList<Project> projectLst = projectService.findAudittedProject(project.getProjectClassId(),
				project.getYear(), project.getName(), page);
		request.setAttribute(IAttrName.PROJECT_LIST, projectLst);
		request.setAttribute(IAttrName.PROJECT_LIST_SIZE, projectLst.size());
		request.setAttribute(IAttrName.CLASS_LIST, ServiceFactory.createProjectClassService().findClassList());
		return Action.SUCCESS;
	}
	
	//
	// 处理修改请求
	//
	public String auditModifyRequestList() throws Exception
	{
		PageList<Project> projectLst = projectService.findRequestModifyProject(project.getProjectClassId(),
				project.getYear(), project.getName(), page);
		request.setAttribute(IAttrName.PROJECT_LIST, projectLst);
		request.setAttribute(IAttrName.PROJECT_LIST_SIZE, projectLst.size());
		request.setAttribute(IAttrName.CLASS_LIST, ServiceFactory.createProjectClassService().findClassList());
		return Action.SUCCESS;
	}
	
	public String auditModifyRequestConfirm() throws Exception
	{
		if(project.getId() == null)
		{
			throw new Exception("未指定项目");
		}
		/**
		 * validation.
		 */
		Project dbProject = projectService.findById(project.getId(), Depth.ZERO);
		if(dbProject == null)
		{
			throw new Exception("指定的项目不存在");
		}
		// Check state.
		int state = dbProject.getState();
		if(state != Project.STATE_REQUEST_MODIFY)
		{
			throw new Exception("没有修改请求");
		}
		/**
		 * update project state.
		 */
		Project newProject = new Project();
		newProject.setId(dbProject.getId());
		newProject.setState(Project.STATE_UNAUDIT_MODIFIABLE);
		projectService.update(newProject);
		return Action.SUCCESS;
	}
	
	public String auditModifyRequestReject() throws Exception
	{
		if(project.getId() == null)
		{
			throw new Exception("未指定项目");
		}
		/**
		 * validation.
		 */
		Project dbProject = projectService.findById(project.getId(), Depth.ZERO);
		if(dbProject == null)
		{
			throw new Exception("指定的项目不存在");
		}
		// Check state.
		int state = dbProject.getState();
		if(state != Project.STATE_REQUEST_MODIFY)
		{
			throw new Exception("没有修改请求");
		}
		/**
		 * update project state.
		 */
		Project newProject = new Project();
		newProject.setId(dbProject.getId());
		newProject.setState(Project.STATE_UNAUDIT_UNMODIFIABLE);
		projectService.update(newProject);
		return Action.SUCCESS;
	}

	
	//
	// Getters and Setters
	//
	@Override
	public Project getModel()
	{
		return project;
	}


	public Project getProject()
	{
		return project;
	}


	public void setProject(Project project)
	{
		this.project = project;
	}

	public Integer getPage()
	{
		return page;
	}

	public void setPage(Integer page)
	{
		this.page = page;
	}
	
	
}
